﻿using Microsoft.EntityFrameworkCore;
using OL.FrameCore.Application.Article.Dto;
using OL.FrameCore.Domain.Entity;
using OL.FrameCore.Infrastructure.UnitOfWork;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using OL.FrameCore.Infrastructure.Utility;

namespace OL.FrameCore.Application.Article
{
    public class ArticleService: IArticleService
    {
        IUnitOfWork _unitOfwork;
        public ArticleService(IUnitOfWork unitOfwork)
        {
            _unitOfwork = unitOfwork;
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PageData<GetArticleListResponse> GetArticleList(GetArticleListRequest request)
        {
            var repositoryArticle = _unitOfwork.GetRepository<AppArticle>();
            var repositoryUser = _unitOfwork.GetRepository<SysUser>();
            var query = from a in repositoryArticle.TableNoTracking
                        join b in repositoryUser.TableNoTracking on a.CreateId equals b.Id
                        where a.IsDelete == false
                        && (request.Id > 0 ? a.Id == request.Id : true)
                        && (string.IsNullOrEmpty(request.Title) ? true : a.Title.Contains(request.Title))
                        select new GetArticleListResponse
                        {
                            Id = a.Id,
                            Title = a.Title,
                            Tags = a.Tags,
                            Status = a.Status,
                            ReleaseTime = a.ReleaseTime,
                            CreateId = a.CreateId,
                            CreateName = b.TrueName,
                            CreateTime = a.CreateTime,
                            UpdateId = a.UpdateId,
                            UpdateName = "",
                            UpdateTime = a.UpdateTime
                        };
            return repositoryArticle.PagingAsync<GetArticleListResponse>(query, request.PageIndex, request.PageSize).Result;
        }
        /// <summary>
        /// 获取文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GetArticleResponse GetArticle(int id)
        {
            var repositoryArticle = _unitOfwork.GetRepository<AppArticle>();
            var query = from a in repositoryArticle.TableNoTracking
                        where a.Id == id
                        select new GetArticleResponse
                        {
                            Id = a.Id,
                            Title = a.Title,
                            Tags = a.Tags,
                            Content = a.Content,
                            Summary = a.Summary
                        };
            return query.SingleOrDefault();
        }
        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Result AddArticle(AddArticleRequest request)
        {
            var repositoryArticle = _unitOfwork.GetRepository<AppArticle>();
            AppArticle article = new AppArticle
            {
                Title = request.Title,
                Content = request.Content,
                Summary = request.Summary,
                Tags = request.Tags,
                CreateId = request.CreateId,
                CreateTime = DateTime.Now,
                UpdateId = request.CreateId,
                UpdateTime = DateTime.Now,
                ReleaseTime = DateTime.Now
            };
            //概要如果为空字符，截取前n个字
            if (string.IsNullOrEmpty(request.Summary))
            {
                article.Summary = StringHandle.GetSummaryString(request.Content, 250);
            }

            //概要图片取文章中第一张图片，没有则用默认图片
            article.SummaryImg = StringHandle.GetFirstImagePaht(request.Content);
            if (string.IsNullOrEmpty(article.SummaryImg))
            {
                article.SummaryImg = "/images/default_img.jpg";
            }
            repositoryArticle.Insert(article);
            _unitOfwork.Save();

            return new SuccessResult();
        }
        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Result UpdateArticle(UpdateArticleRequest request)
        {
            var repositoryArticle = _unitOfwork.GetRepository<AppArticle>();
            var article = repositoryArticle.Table.SingleOrDefault(p => p.Id == request.Id);
            if (article == null)
                return new FailResult("找不到文章");

            article.Title = request.Title;
            article.Content = request.Content;
            article.Tags = request.Tags;
            article.Summary = request.Summary;
            //概要如果为空字符，截取前n个字
            if (string.IsNullOrEmpty(request.Summary))
            {
                article.Summary = StringHandle.GetSummaryString(request.Content, 250);
            }

            //概要图片取文章中第一张图片，没有则用默认图片
            article.SummaryImg = StringHandle.GetFirstImagePaht(request.Content);
            if (string.IsNullOrEmpty(article.SummaryImg))
            {
                article.SummaryImg = "/images/default_img.jpg";
            }
            article.Status = 0;//设置状态初始
            article.UpdateId = request.UpdateId;
            article.UpdateTime = DateTime.Now;

            _unitOfwork.Save();

            return new SuccessResult();
        }
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <returns></returns>
        public Result DeleteArticle(int id, int userId)
        {
            var repositoryArticle = _unitOfwork.GetRepository<AppArticle>();
            var article = repositoryArticle.Table.SingleOrDefault(p => p.Id == id && p.IsDelete == false);
            if (article == null)
                return new FailResult("找不到文章");

            article.IsDelete = true;
            article.UpdateId = userId;
            article.UpdateTime = DateTime.Now;
            _unitOfwork.Save();

            return new SuccessResult();
        }

        /// <summary>
        /// 获取文章浏览列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<GetArticleViewListResponse> GetArticleViewList(int type = 1,int num = 10)
        {
            var repositoryArticle = _unitOfwork.GetRepository<AppArticle>();
            var query = from a in repositoryArticle.TableNoTracking
                        where a.IsDelete == false
                        orderby a.ReleaseTime descending
                        select new GetArticleViewListResponse
                        {
                            Id = a.Id,
                            Title = a.Title,
                            CreateTime = a.ReleaseTime
                        };
            return query.Take(num).ToList();
        }
    }
}
